//
//  AY_BookQianDCell1.m
//  AiYue
//
//  Created by boWen on 2023/11/2.
//

#import "AY_BookQianDCell1.h"
#import "AY_BookQDItemCell.h"
#import "AY_BookQDItemCell2.h"
#import "AY_BookLianXuAlertView.h"
#import "AY_BookVpPage.h"
@interface AY_BookQianDCell1  ()<UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UIView * bg_View;//底部圆角
@property (nonatomic, strong) UICollectionView * myCollectionView;

@property (nonatomic, strong) NSMutableArray * sourceAry;//数据源
@property (nonatomic, strong) AY_BookLianXuAlertView * qdlxAlertView;

@end

@implementation AY_BookQianDCell1


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}


- (void)addAllSubviewsWithUI {
    self.sourceAry = [NSMutableArray array];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:0 andAddSubView:self.contentView];
    self.bg_View.layer.masksToBounds = YES;
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.contentView);
    }];
    
//    [self.bg_View layoutIfNeeded];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_W, YNUM(233)) byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(YNUM(13), YNUM(13))];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = CGRectMake(0, 0, SCREEN_W, YNUM(233));
    maskLayer.path = maskPath.CGPath;
    self.bg_View.layer.mask = maskLayer;
    
    [self.bg_View addSubview:self.myCollectionView];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bg_View).offset(YNUM(14));
        make.right.equalTo(self.bg_View).offset(YNUM(-14));
        make.top.equalTo(self.bg_View).offset(YNUM(18));
        make.bottom.equalTo(self.bg_View).offset(YNUM(-18));
    }];
}

- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumLineSpacing = 9;
        layout.minimumInteritemSpacing = 9;
       UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.scrollEnabled = NO;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[AY_BookQDItemCell class] forCellWithReuseIdentifier:@"AY_BookQDItemCell"];
        [collectionView registerClass:[AY_BookQDItemCell2 class] forCellWithReuseIdentifier:@"AY_BookQDItemCell2"];
        _myCollectionView = collectionView;
    }
    return _myCollectionView;
}

#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.qdList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == self.qdList.count - 1) {
        AY_BookQDItemCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookQDItemCell2" forIndexPath:indexPath];
        if(self.qdList.count > 0) {
            cell.qdListModel = self.qdList[self.qdList.count - 1];
        }
        return cell;
    }else {
        AY_BookQDItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookQDItemCell" forIndexPath:indexPath];
        if(self.qdList.count > indexPath.row) {
            cell.qdListModel = self.qdList[indexPath.row];
        }
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.row == self.qdList.count - 1 ? CGSizeMake(YNUM(153), YNUM(94)) : CGSizeMake(YNUM(72), YNUM(94));
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 9;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    QianDListModel * qdModel = self.qdList[indexPath.row];
    if([kStringUtils(_signModel.canBackSign) isEqualToString:@"1"] && [kStringUtils(qdModel.signedFlag) isEqualToString:@"0"]) {
        self.qdlxAlertView.rewordNeedjib = self.signModel.backSignGold;
        [self.qdlxAlertView showPopView];
    }
}

- (void)setQdList:(NSArray *)qdList {
    _qdList = qdList;
    [self.myCollectionView reloadData];
}

#pragma mark - 自定义事件

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 16;
    frame.size.width -= 2 * 16;
    [super setFrame:frame];
}


//获取用户补签
- (void)getQdMessage {
    [AYApiRequest getBackSignUpSuccess:^(AY_SignUpModel * _Nonnull pkg) {
        if([pkg.code isEqualToString:@"200"]) {
            [self.myCollectionView reloadData];
            if(self.refreshBlock) {
                self.refreshBlock();
            }
        }else {
            ShowToast(pkg.msg);
        }
    } failure:^(int status, NSString * _Nonnull info) {
        ShowToast(@"亲，网络不顺畅哟~");
    }];
}

- (AY_BookLianXuAlertView *)qdlxAlertView {
    if(!_qdlxAlertView) {
        _qdlxAlertView = [[AY_BookLianXuAlertView alloc] initWithFrame:CGRectMake(0, 0, GK_SCREEN_WIDTH, GK_SCREEN_HEIGHT)];
        Weak_Self
        _qdlxAlertView.buqianBlock = ^{
            [weakSelf getQdMessage];
        };
        _qdlxAlertView.buqianVpBlock = ^{
            AY_BookVpPage * page = [[AY_BookVpPage alloc] init];
            [[AYTool viewControllerWith:weakSelf].navigationController pushViewController:page animated:YES];
        };
    }
    return _qdlxAlertView;
}



@end
